/*     */ package com.zimbra.cs.dav;
/*     */ 
/*     */ import com.zimbra.client.ZFolder;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.Options;
/*     */ import com.zimbra.client.ZMountpoint;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.dav.resource.DavResource;
/*     */ import com.zimbra.cs.dav.resource.UrlNamespace;
/*     */ import com.zimbra.cs.dav.resource.UrlNamespace.UrlComponents;
/*     */ import com.zimbra.cs.dav.service.DavResponse;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.FileUploadServlet;
/*     */ import com.zimbra.cs.service.FileUploadServlet.Upload;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import java.io.IOException;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URI;
/*     */ import java.net.URLDecoder;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Enumeration;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentException;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.QName;
/*     */ import org.dom4j.io.SAXReader;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DavContext
/*     */ {
/*     */   private static HashSet<Extension> sExtensions;
/*     */   private final HttpServletRequest mReq;
/*     */   private final HttpServletResponse mResp;
/*     */   private final OperationContext mOpCtxt;
/*     */   private final Account mAuthAccount;
/*     */   private String mUri;
/*     */   private String mUser;
/*     */   private String mPath;
/*     */   private int mStatus;
/*     */   private Document mRequestMsg;
/*     */   private FileUploadServlet.Upload mUpload;
/*     */   private DavResponse mResponse;
/*     */   private boolean mResponseSent;
/*     */   private DavResource mRequestedResource;
/*     */   private com.zimbra.cs.dav.resource.Collection mRequestedParentCollection;
/*     */   private RequestType mRequestType;
/*     */   private String mCollectionPath;
/*     */   private RequestProp mResponseProp;
/*     */   private String mDavCompliance;
/*     */   
/*     */   static
/*     */   {
/*  81 */     synchronized (DavContext.class) {
/*  82 */       if (sExtensions == null)
/*  83 */         sExtensions = new HashSet();
/*     */     }
/*     */   }
/*     */   
/*  87 */   public static void addExtension(Extension ex) { synchronized (sExtensions) {
/*  88 */       sExtensions.add(ex);
/*     */     }
/*     */   }
/*     */   
/*  92 */   public static void removeExtension(Extension ex) { synchronized (sExtensions) {
/*  93 */       sExtensions.remove(ex);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 121 */   private String actingAsDelegateFor = null;
/*     */   
/*     */   public static abstract interface Extension { public abstract void callback(DavContext paramDavContext); }
/*     */   
/* 125 */   private static enum RequestType { PRINCIPAL,  RESOURCE;
/*     */     
/*     */     private RequestType() {}
/*     */   }
/*     */   
/*     */   public static class RequestProp { boolean nameOnly;
/*     */     boolean allProp;
/*     */     HashMap<QName, org.dom4j.Element> props;
/*     */     HashMap<QName, DavException> errProps;
/*     */     
/* 135 */     public RequestProp(boolean no) { this.props = new HashMap();
/* 136 */       this.errProps = new HashMap();
/* 137 */       this.nameOnly = no;
/* 138 */       this.allProp = true;
/*     */     }
/*     */     
/*     */     public RequestProp(org.dom4j.Element top) {
/* 142 */       this(false);
/*     */       
/* 144 */       this.allProp = false;
/* 145 */       for (Object obj : top.elements())
/* 146 */         if ((obj instanceof org.dom4j.Element))
/*     */         {
/* 148 */           org.dom4j.Element e = (org.dom4j.Element)obj;
/* 149 */           String name = e.getName();
/* 150 */           if (name.equals("allprop")) {
/* 151 */             this.allProp = true;
/* 152 */           } else if (name.equals("propname")) {
/* 153 */             this.nameOnly = true;
/* 154 */           } else if (name.equals("prop"))
/*     */           {
/* 156 */             List<org.dom4j.Element> propElems = e.elements();
/* 157 */             for (org.dom4j.Element prop : propElems)
/* 158 */               this.props.put(prop.getQName(), prop);
/*     */           }
/*     */         }
/*     */     }
/*     */     
/*     */     public RequestProp(java.util.Collection<org.dom4j.Element> set, java.util.Collection<QName> remove) {
/* 164 */       this(false);
/* 165 */       this.allProp = false;
/* 166 */       for (org.dom4j.Element e : set)
/* 167 */         this.props.put(e.getQName(), e);
/* 168 */       for (QName q : remove)
/* 169 */         this.props.put(q, DocumentHelper.createElement(q));
/*     */     }
/*     */     
/*     */     public boolean isNameOnly() {
/* 173 */       return this.nameOnly;
/*     */     }
/*     */     
/* 176 */     public boolean isAllProp() { return this.allProp; }
/*     */     
/*     */     public void addProp(org.dom4j.Element p) {
/* 179 */       this.allProp = false;
/* 180 */       this.props.put(p.getQName(), p);
/*     */     }
/*     */     
/* 183 */     public java.util.Collection<QName> getProps() { return this.props.keySet(); }
/*     */     
/*     */ 
/* 186 */     public org.dom4j.Element getProp(QName p) { return (org.dom4j.Element)this.props.get(p); }
/*     */     
/*     */     public void addPropError(QName prop, DavException ex) {
/* 189 */       this.allProp = false;
/* 190 */       if (!this.props.containsKey(prop))
/* 191 */         this.props.put(prop, DocumentHelper.createElement(prop));
/* 192 */       this.errProps.put(prop, ex);
/*     */     }
/*     */     
/* 195 */     public Map<QName, DavException> getErrProps() { return this.errProps; }
/*     */   }
/*     */   
/*     */   public RequestProp getRequestProp() throws DavException {
/* 199 */     if (hasRequestMessage()) {
/* 200 */       Document req = getRequestMessage();
/* 201 */       return new RequestProp(req.getRootElement());
/*     */     }
/* 203 */     return sEmptyProp;
/*     */   }
/*     */   
/*     */   public RequestProp getResponseProp() {
/* 207 */     return this.mResponseProp;
/*     */   }
/*     */   
/*     */   public void setResponseProp(RequestProp props) {
/* 211 */     this.mResponseProp = props;
/*     */   }
/*     */   
/*     */ 
/*     */   private boolean mOverwrite;
/*     */   private boolean mBrief;
/* 217 */   protected static RequestProp sEmptyProp = new RequestProp(false);
/*     */   private static final String EVOLUTION = "Evolution";
/*     */   
/*     */   public DavContext(DavContext copy, String path) {
/* 221 */     this.mReq = copy.mReq;
/* 222 */     this.mResp = copy.mResp;
/* 223 */     this.mAuthAccount = copy.mAuthAccount;
/* 224 */     this.mOpCtxt = copy.mOpCtxt;
/* 225 */     this.mUser = copy.mUser;
/* 226 */     this.mPath = path;
/*     */   }
/*     */   
/*     */   public DavContext(HttpServletRequest req, HttpServletResponse resp, Account authUser) {
/* 230 */     this.mReq = req;this.mResp = resp;
/* 231 */     this.mUri = req.getPathInfo();
/* 232 */     if ((this.mUri != null) && (this.mUri.length() > 1))
/*     */     {
/* 234 */       if ((this.mUri.toLowerCase().endsWith(".ics")) || (this.mUri.toLowerCase().endsWith(".vcf"))) {
/* 235 */         String rawUri = req.getRequestURI();
/* 236 */         String[] fragments = HttpUtil.getPathFragments(URI.create(rawUri));
/*     */         
/* 238 */         if ((!req.getContextPath().isEmpty()) && (fragments.length > 0) && (HttpUtil.urlUnescape(req.getContextPath()).equals("/" + fragments[0])))
/*     */         {
/* 240 */           fragments = (String[])Arrays.copyOfRange(fragments, 1, fragments.length);
/*     */         }
/* 242 */         String servletPath = req.getServletPath();
/* 243 */         if (!servletPath.isEmpty()) {
/* 244 */           String[] servletPathFragments = HttpUtil.getPathFragments(URI.create(servletPath));
/* 245 */           for (String servletPathFragment : servletPathFragments) {
/* 246 */             if ((fragments.length > 0) && (servletPathFragment.equals(fragments[0]))) {
/* 247 */               fragments = (String[])Arrays.copyOfRange(fragments, 1, fragments.length);
/*     */             }
/*     */           }
/*     */         }
/*     */         
/* 252 */         fragments[(fragments.length - 1)] = HttpUtil.urlEscapeIncludingSlash(fragments[(fragments.length - 1)]);
/* 253 */         URI uri = HttpUtil.getUriFromFragments(fragments, req.getQueryString(), true, false);
/* 254 */         this.mUri = uri.getPath();
/*     */       }
/*     */       
/* 257 */       int index = this.mUri.indexOf('/', 1);
/* 258 */       if (index > 0) {
/* 259 */         String reqType = this.mUri.substring(1, index);
/* 260 */         int start = index + 1;
/* 261 */         if (reqType.equals("home")) {
/* 262 */           this.mRequestType = RequestType.RESOURCE;
/* 263 */         } else if (this.mUri.startsWith("/principals/users/")) {
/* 264 */           this.mRequestType = RequestType.PRINCIPAL;
/* 265 */           start = "/principals/users/".length();
/*     */         }
/* 267 */         index = this.mUri.indexOf('/', start);
/* 268 */         if (index != -1) {
/* 269 */           this.mUser = this.mUri.substring(start, index);
/* 270 */           this.mPath = this.mUri.substring(index);
/*     */         } else {
/* 272 */           this.mUser = this.mUri.substring(start);
/* 273 */           this.mPath = "/";
/*     */         }
/* 275 */         index = this.mPath.lastIndexOf('/');
/* 276 */         if ((index < this.mPath.length() - 1) && (index > 0))
/* 277 */           this.mCollectionPath = this.mPath.substring(0, index);
/*     */       }
/*     */     }
/* 280 */     this.mStatus = 200;
/* 281 */     this.mAuthAccount = authUser;
/* 282 */     this.mOpCtxt = new OperationContext(authUser);
/* 283 */     this.mOpCtxt.setUserAgent(req.getHeader("User-Agent"));
/* 284 */     this.mDavCompliance = DavProtocol.getDefaultComplianceString();
/* 285 */     String overwrite = this.mReq.getHeader("Overwrite");
/* 286 */     this.mOverwrite = ((overwrite == null) || (!overwrite.equals("F")));
/* 287 */     String brief = this.mReq.getHeader("Brief");
/* 288 */     this.mBrief = ((brief != null) && (brief.equals("t")));
/*     */   }
/*     */   
/*     */   public HttpServletRequest getRequest()
/*     */   {
/* 293 */     return this.mReq;
/*     */   }
/*     */   
/*     */   public HttpServletResponse getResponse()
/*     */   {
/* 298 */     return this.mResp;
/*     */   }
/*     */   
/*     */   public OperationContext getOperationContext()
/*     */   {
/* 303 */     return this.mOpCtxt;
/*     */   }
/*     */   
/*     */   public Account getAuthAccount()
/*     */   {
/* 308 */     return this.mAuthAccount;
/*     */   }
/*     */   
/*     */ 
/*     */   private static final String ICAL = "iCal/";
/*     */   
/*     */   private static final String IPHONE = "iPhone/";
/*     */   
/*     */   private static final String ADDRESSBOOK = "Address";
/*     */   
/*     */   private static final String MICROSOFT = "Microsoft";
/*     */   
/*     */   private static final String MSIE = "MSIE";
/*     */   private static final String MOZILLA = "Mozilla";
/*     */   public String getUri()
/*     */   {
/* 324 */     return this.mUri;
/*     */   }
/*     */   
/*     */   public String getUser() {
/* 328 */     return this.mUser;
/*     */   }
/*     */   
/*     */   public String getPath() {
/* 332 */     return this.mPath;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public String getItem()
/*     */   {
/* 339 */     if (this.mPath != null) {
/* 340 */       if (this.mPath.equals("/")) {
/* 341 */         return this.mPath;
/*     */       }
/*     */       
/* 344 */       if (this.mPath.endsWith("/")) {
/* 345 */         int length = this.mPath.length();
/* 346 */         int index = this.mPath.lastIndexOf('/', length - 2);
/* 347 */         if (index != -1) {
/* 348 */           return this.mPath.substring(index + 1, length - 1);
/*     */         }
/*     */       } else {
/* 351 */         int index = this.mPath.lastIndexOf('/');
/* 352 */         if (index != -1) {
/* 353 */           return this.mPath.substring(index + 1);
/*     */         }
/*     */       }
/*     */     }
/* 357 */     return null;
/*     */   }
/*     */   
/*     */   public String getActingAsDelegateFor() {
/* 361 */     return this.actingAsDelegateFor;
/*     */   }
/*     */   
/* 364 */   public void setActingAsDelegateFor(String name) { this.actingAsDelegateFor = name; }
/*     */   
/*     */   public String getCollectionPath()
/*     */   {
/* 368 */     return this.mCollectionPath;
/*     */   }
/*     */   
/* 371 */   public void setCollectionPath(String collPath) { this.mCollectionPath = collPath; }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public int getStatus()
/*     */   {
/* 378 */     return this.mStatus;
/*     */   }
/*     */   
/*     */   public void setStatus(int s) {
/* 382 */     this.mStatus = s;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void responseSent()
/*     */   {
/* 390 */     this.mResponseSent = true;
/*     */   }
/*     */   
/*     */   public boolean isResponseSent() {
/* 394 */     return this.mResponseSent;
/*     */   }
/*     */   
/*     */   public static enum Depth
/*     */   {
/* 399 */     zero,  one,  infinity;
/*     */     
/*     */     private Depth() {} }
/*     */   
/* 403 */   public Depth getDepth() { String hdr = this.mReq.getHeader("Depth");
/* 404 */     if (hdr == null)
/* 405 */       return Depth.zero;
/* 406 */     if (hdr.equals("0"))
/* 407 */       return Depth.zero;
/* 408 */     if (hdr.equals("1"))
/* 409 */       return Depth.one;
/* 410 */     if (hdr.equalsIgnoreCase("infinity")) {
/* 411 */       return Depth.infinity;
/*     */     }
/* 413 */     ZimbraLog.dav.info("invalid depth: " + hdr);
/* 414 */     return Depth.zero;
/*     */   }
/*     */   
/*     */   public boolean hasRequestMessage()
/*     */   {
/*     */     try {
/* 420 */       String ct = getUpload().getContentType();
/* 421 */       return (getUpload().getSize() > 0L) && (ct != null) && ((ct.startsWith("text/xml")) || (ct.startsWith("application/xml")));
/*     */     }
/*     */     catch (Exception e) {}
/* 424 */     return false;
/*     */   }
/*     */   
/*     */   public FileUploadServlet.Upload getUpload() throws DavException, IOException {
/* 428 */     if (this.mUpload == null) {
/* 429 */       String name = null;
/* 430 */       String ctype = getRequest().getContentType();
/* 431 */       if (ctype == null)
/* 432 */         name = getItem();
/*     */       try {
/* 434 */         this.mUpload = FileUploadServlet.saveUpload(this.mReq.getInputStream(), name, ctype, this.mAuthAccount.getId(), true);
/* 435 */         ZimbraLog.dav.debug("Request: requested content-type: %s, actual content-type: %s", new Object[] { ctype, this.mUpload.getContentType() });
/*     */       } catch (ServiceException se) {
/* 437 */         throw new DavException("can't save upload", 413, se);
/*     */       }
/*     */     }
/* 440 */     return this.mUpload;
/*     */   }
/*     */   
/*     */   public void cleanup() {
/* 444 */     if (sExtensions.size() > 0)
/* 445 */       for (Extension ex : sExtensions)
/* 446 */         ex.callback(this);
/* 447 */     if (this.mUpload != null)
/* 448 */       FileUploadServlet.deleteUpload(this.mUpload);
/* 449 */     this.mUpload = null;
/*     */   }
/*     */   
/*     */   public Document getRequestMessage() throws DavException
/*     */   {
/* 454 */     if (this.mRequestMsg != null)
/* 455 */       return this.mRequestMsg;
/*     */     try {
/* 457 */       if (hasRequestMessage()) {
/* 458 */         this.mRequestMsg = com.zimbra.common.soap.Element.getSAXReader().read(getUpload().getInputStream());
/* 459 */         return this.mRequestMsg;
/*     */       }
/*     */     } catch (DocumentException e) {
/* 462 */       throw new DavException("unable to parse request message", 400, e);
/*     */     } catch (IOException e) {
/* 464 */       throw new DavException("can't read uploaded file", 500, e);
/*     */     }
/* 466 */     throw new DavException("no request msg", 400, null);
/*     */   }
/*     */   
/*     */   public boolean hasResponseMessage()
/*     */   {
/* 471 */     return this.mResponse != null;
/*     */   }
/*     */   
/*     */   public DavResponse getDavResponse()
/*     */   {
/* 476 */     if (this.mResponse == null)
/* 477 */       this.mResponse = new DavResponse();
/* 478 */     return this.mResponse;
/*     */   }
/*     */   
/*     */   public DavResource getRequestedResource() throws DavException, ServiceException {
/* 482 */     if (this.mRequestedResource == null) {
/* 483 */       if (this.mRequestType == RequestType.RESOURCE) {
/* 484 */         this.mRequestedResource = UrlNamespace.getResourceAt(this, this.mUser, this.mPath);
/*     */       } else
/* 486 */         this.mRequestedResource = UrlNamespace.getPrincipalAtUrl(this, this.mUri);
/* 487 */       if (this.mRequestedResource != null)
/* 488 */         ZimbraLog.addToContext("name", this.mRequestedResource.getOwner());
/*     */     }
/* 490 */     if (this.mRequestedResource == null)
/* 491 */       throw new DavException("no DAV resource at " + this.mUri, 404, null);
/* 492 */     return this.mRequestedResource;
/*     */   }
/*     */   
/*     */   public com.zimbra.cs.dav.resource.Collection getRequestedParentCollection() throws DavException, ServiceException {
/* 496 */     if ((this.mPath != null) && (this.mRequestedParentCollection == null))
/* 497 */       this.mRequestedParentCollection = UrlNamespace.getCollectionAtUrl(this, this.mPath);
/* 498 */     return this.mRequestedParentCollection;
/*     */   }
/*     */   
/*     */   public java.util.Collection<DavResource> getAllRequestedResources() throws DavException, ServiceException {
/* 502 */     ArrayList<DavResource> rss = new ArrayList();
/* 503 */     if (this.mRequestType == RequestType.RESOURCE) {
/* 504 */       rss = (ArrayList)UrlNamespace.getResources(this, this.mUser, this.mPath, getDepth() == Depth.one);
/*     */     } else {
/* 506 */       DavResource rs = UrlNamespace.getPrincipalAtUrl(this, this.mUri);
/* 507 */       if (rs != null) {
/* 508 */         ZimbraLog.addToContext("name", rs.getOwner());
/* 509 */         rss.add(rs);
/* 510 */         if (getDepth() == Depth.one)
/* 511 */           rss.addAll(rs.getChildren(this));
/*     */       }
/*     */     }
/* 514 */     if (rss.isEmpty())
/* 515 */       throw new DavException("no DAV resource at " + this.mUri, 404, null);
/* 516 */     return rss;
/*     */   }
/*     */   
/*     */   public Mailbox getTargetMailbox() throws ServiceException {
/* 520 */     Account acct = Provisioning.getInstance().getAccountByName(this.mUser);
/* 521 */     if (acct == null)
/* 522 */       return null;
/* 523 */     return MailboxManager.getInstance().getMailboxByAccount(acct);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private boolean userAgentHeaderContains(String str)
/*     */   {
/* 535 */     String userAgent = this.mReq.getHeader("User-Agent");
/* 536 */     if (userAgent == null)
/* 537 */       return false;
/* 538 */     return userAgent.indexOf(str) >= 0;
/*     */   }
/*     */   
/*     */   public boolean isAddressbookClient() {
/* 542 */     return userAgentHeaderContains("Address");
/*     */   }
/*     */   
/*     */   public boolean isIcalClient() {
/* 546 */     return userAgentHeaderContains("iCal/");
/*     */   }
/*     */   
/*     */   public boolean isMsft() {
/* 550 */     return userAgentHeaderContains("Microsoft");
/*     */   }
/*     */   
/*     */   public boolean isWebRequest() {
/* 554 */     return (userAgentHeaderContains("MSIE")) || (userAgentHeaderContains("Mozilla"));
/*     */   }
/*     */   
/*     */   public static enum KnownUserAgent {
/* 558 */     iCal,  iPhone,  Evolution;
/*     */     
/*     */     private KnownUserAgent() {}
/* 561 */     static KnownUserAgent lookup(String userAgent) { if (userAgent != null) {
/* 562 */         if (userAgent.indexOf("iPhone/") >= 0)
/* 563 */           return iPhone;
/* 564 */         if (userAgent.indexOf("iCal/") >= 0)
/* 565 */           return iCal;
/* 566 */         if (userAgent.indexOf("Evolution") >= 0)
/* 567 */           return Evolution;
/*     */       }
/* 569 */       return null;
/*     */     }
/*     */   }
/*     */   
/*     */   public KnownUserAgent getKnownUserAgent() {
/* 574 */     String userAgent = this.mReq.getHeader("User-Agent");
/* 575 */     return KnownUserAgent.lookup(userAgent);
/*     */   }
/*     */   
/*     */   public boolean isFreebusyEnabled() {
/*     */     try {
/* 580 */       return !Provisioning.getInstance().getConfig().getBooleanAttr("zimbraCalendarCalDavDisableFreebusy", false);
/*     */     } catch (ServiceException se) {}
/* 582 */     return false;
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean isGzipAccepted()
/*     */   {
/* 588 */     Enumeration acceptEncHdrs = this.mReq.getHeaders("Accept-Encoding");
/* 589 */     while (acceptEncHdrs.hasMoreElements()) {
/* 590 */       String acceptEnc = (String)acceptEncHdrs.nextElement();
/*     */       
/* 592 */       if ((acceptEnc != null) && (acceptEnc.toLowerCase().contains("gzip")))
/* 593 */         return true;
/*     */     }
/* 595 */     return false;
/*     */   }
/*     */   
/*     */   public void setDavCompliance(String comp) {
/* 599 */     this.mDavCompliance = comp;
/*     */   }
/*     */   
/*     */   public String getDavCompliance() {
/* 603 */     return this.mDavCompliance;
/*     */   }
/*     */   
/*     */   public boolean useIcalDelegation() {
/* 607 */     if (this.mAuthAccount != null)
/* 608 */       return this.mAuthAccount.isPrefAppleIcalDelegationEnabled();
/* 609 */     return false;
/*     */   }
/*     */   
/*     */   public boolean isOverwriteSet() {
/* 613 */     return this.mOverwrite;
/*     */   }
/*     */   
/*     */   public boolean isBrief() {
/* 617 */     return this.mBrief;
/*     */   }
/*     */   
/*     */   public com.zimbra.cs.dav.resource.Collection getDestinationCollection() throws DavException {
/* 621 */     String destinationUrl = getDestinationUrl();
/* 622 */     if (!destinationUrl.endsWith("/")) {
/* 623 */       int slash = destinationUrl.lastIndexOf('/');
/* 624 */       destinationUrl = destinationUrl.substring(0, slash + 1);
/*     */     }
/*     */     try {
/* 627 */       destinationUrl = HttpUtil.urlUnescape(destinationUrl);
/* 628 */       destinationUrl = getInternalDestinationUrl(destinationUrl);
/* 629 */       DavResource r = UrlNamespace.getResourceAtUrl(this, destinationUrl);
/* 630 */       if ((r instanceof com.zimbra.cs.dav.resource.Collection))
/* 631 */         return (com.zimbra.cs.dav.resource.Collection)r;
/* 632 */       return UrlNamespace.getCollectionAtUrl(this, destinationUrl);
/*     */     } catch (Exception e) {
/* 634 */       throw new DavException("can't get destination collection", 424);
/*     */     }
/*     */   }
/*     */   
/*     */   private String getDestinationUrl() throws DavException {
/* 639 */     String destination = getRequest().getHeader("Destination");
/* 640 */     if (destination == null)
/* 641 */       throw new DavException("no destination specified", 400, null);
/* 642 */     return destination;
/*     */   }
/*     */   
/*     */   private String getInternalDestinationUrl(String destinationUrl) throws ServiceException, DavException {
/* 646 */     UrlNamespace.UrlComponents uc = UrlNamespace.parseUrl(destinationUrl);
/* 647 */     Account targetAcct = Provisioning.getInstance().getAccountByName(uc.user);
/* 648 */     if (targetAcct == null)
/* 649 */       return destinationUrl;
/* 650 */     ZMailbox zmbx = getZMailbox(targetAcct);
/* 651 */     ItemId targetRoot = new ItemId("", 1);
/* 652 */     Pair<ZFolder, String> match = zmbx.getFolderByPathLongestMatch(targetRoot.toString(), uc.path);
/* 653 */     ZFolder targetFolder = (ZFolder)match.getFirst();
/* 654 */     if ((targetFolder instanceof ZMountpoint)) {
/* 655 */       ZMountpoint zmp = (ZMountpoint)targetFolder;
/* 656 */       ItemId target = new ItemId(zmp.getOwnerId(), Integer.parseInt(zmp.getRemoteId()));
/* 657 */       Account acct = Provisioning.getInstance().getAccountById(zmp.getOwnerId());
/* 658 */       ZMailbox targetZmbx = getZMailbox(acct);
/* 659 */       ZFolder f = targetZmbx.getFolderById(target.toString());
/* 660 */       String extraPath = (String)match.getSecond();
/* 661 */       destinationUrl = "/dav/" + acct.getName() + f.getPath() + (extraPath != null ? "/" + extraPath : "");
/*     */     }
/* 663 */     return destinationUrl;
/*     */   }
/*     */   
/*     */   private ZMailbox getZMailbox(Account acct) throws ServiceException
/*     */   {
/* 668 */     AuthToken authToken = AuthProvider.getAuthToken(getAuthAccount());
/* 669 */     ZMailbox.Options zoptions = new ZMailbox.Options(authToken.toZAuthToken(), AccountUtil.getSoapUri(acct));
/* 670 */     zoptions.setNoSession(true);
/* 671 */     zoptions.setTargetAccount(acct.getId());
/* 672 */     zoptions.setTargetAccountBy(Key.AccountBy.id);
/* 673 */     return ZMailbox.getMailbox(zoptions);
/*     */   }
/*     */   
/*     */   public String getNewName() throws DavException {
/* 677 */     String oldName = getItem();
/* 678 */     String dest = getDestinationUrl();
/*     */     
/* 680 */     int end = dest.length();
/* 681 */     if (dest.endsWith("/"))
/* 682 */       end--;
/* 683 */     int begin = dest.lastIndexOf("/", end - 1);
/* 684 */     String newName = dest.substring(begin + 1, end);
/*     */     try {
/* 686 */       newName = URLDecoder.decode(newName, "UTF-8");
/*     */     } catch (UnsupportedEncodingException e) {
/* 688 */       ZimbraLog.dav.warn("can't decode URL ", dest, e);
/*     */     }
/* 690 */     if (!oldName.equals(newName)) {
/* 691 */       return newName;
/*     */     }
/* 693 */     return null;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/DavContext.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */